Imports System.Text.RegularExpressions

Namespace Main.Forms
    Public Class FmMain

#Region "Declares"
        Private _mRegex As String
        Private _mReplace As String
#End Region

#Region "Form Events"
        Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
            With OpenFileDialog
                .Filter = "All Files|*.*"
                .InitialDirectory = Application.ExecutablePath
                If (.ShowDialog() = DialogResult.OK) Then
                    lstbFiles.Items.Clear()
                    lstbTobe.Items.Clear()
                    Dim sPath As String = ""
                    For Each fpath As String In .FileNames
                        If (sPath.Length <= 0) Then
                            sPath = IO.Path.GetDirectoryName(fpath)
                        End If
                        If (lstbFiles.Tag Is Nothing) Then
                            lstbFiles.Tag = sPath
                        Else
                            If (CStr(lstbFiles.Tag) <> sPath) Then
                                lstbFiles.Tag = sPath
                            End If
                        End If
                        lstbFiles.Items.Add(IO.Path.GetFileName(fpath))
                    Next
                End If
            End With
        End Sub

        Private Sub btnRule_Click(sender As Object, e As EventArgs) Handles btnRule.Click
            If (lstbFiles.SelectedIndex < 0) Then
                lstbFiles.Select()
                Exit Sub
            End If
            Dim fm As fmRules = New fmRules()
            With fm
                .FileName = lstbFiles.Items(lstbFiles.SelectedIndex)
                If (_mRegex.Length > 0) Then
                    .sRegex = _mRegex
                End If
                .sReplace = _mReplace
                If (.ShowDialog() = DialogResult.OK) Then
                    _mRegex = .sRegex
                    _mReplace = .sReplace
                    .Hide()
                    doPreViewListBox()
                End If
                .Dispose()
            End With
        End Sub

        Private Sub btnChange_Click(sender As Object, e As EventArgs) Handles btnChange.Click
            If (lstbTobe.Items.Count > 0) Then
                doChangeFileName()
            End If
        End Sub

        Private Sub lstbFiles_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstbFiles.MouseDoubleClick
            btnRule.PerformClick()
        End Sub

        Private Sub fmMain_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
            btnClear.PerformClick()
        End Sub

        Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
            lstbFiles.Items.Clear()
            lstbTobe.Items.Clear()
            _mRegex = ""
            _mReplace = ""
            ToolStripStatusLabel.Text = ""
            ToolStripProgressBar.Value = 0
        End Sub

        Private Sub btnPlus_Click(sender As Object, e As EventArgs) Handles btnPlus.Click
            Dim cnt As Integer
            Try
                cnt = CInt(txtDigitalCnt.Text)
                cnt += 1
            Catch ex As Exception
                cnt = 2
            End Try
            txtDigitalCnt.Text = cnt.ToString()
            doPreViewListBox()
        End Sub

        Private Sub btnMinus_Click(sender As Object, e As EventArgs) Handles btnMinus.Click
            Dim cnt As Integer
            Try
                cnt = CInt(txtDigitalCnt.Text)
                cnt -= 1
                If (cnt < 1) Then
                    cnt = 1
                End If
            Catch ex As Exception
                cnt = 2
            End Try
            txtDigitalCnt.Text = cnt.ToString()
            doPreViewListBox()
        End Sub
#End Region

#Region "Methods"
        Private Sub DoPreViewListBox()
            If (lstbFiles.Items.Count <= 0) Then
                Exit Sub
            End If
            Dim nDig As Integer = CInt(txtDigitalCnt.Text)
            lstbTobe.Items.Clear()
            ToolStripProgressBar.Maximum = lstbFiles.Items.Count
            ToolStripProgressBar.Value = 0
            For Each item As String In lstbFiles.Items
                Dim rx As Regex = New Regex(_mRegex)
                If (_mRegex.IndexOf("%d", StringComparison.Ordinal) >= 0) Then
                    ToolStripStatusLabel.Text = _mReplace.Replace("%d", (lstbFiles.Items.IndexOf(item) + 1).ToString.PadLeft(nDig, "0"))
                Else
                    ToolStripStatusLabel.Text = rx.Replace(item, _mReplace)
                End If
                lstbTobe.Items.Add(ToolStripStatusLabel.Text)
                ToolStripProgressBar.Value = ToolStripProgressBar.Value + 1
                Application.DoEvents()
            Next
        End Sub

        Private Sub DoChangeFileName()
            Dim sPath As String = lstbFiles.Tag
            ToolStripProgressBar.Maximum = lstbFiles.Items.Count
            ToolStripProgressBar.Value = 0
            For i As Integer = 0 To lstbFiles.Items.Count - 1
                Dim sOrg As String = sPath + "\" + lstbFiles.Items(i)
                Dim sDst As String = lstbTobe.Items(i)
                Try
                    ToolStripStatusLabel.Text = "=>" + sDst
                    ToolStripProgressBar.Value = ToolStripProgressBar.Value + 1
                    My.Computer.FileSystem.RenameFile(sOrg, sDst)
                Catch ex As Exception
                    MsgBox(ex.Message)
                    Exit For
                End Try
                Application.DoEvents()
            Next
        End Sub


#End Region




    End Class

End Namespace
